﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GT.Extensions.Helpers
{
  public class StringHelper
  {
    /// <summary>Takes a fully qualified file name, and returns just the path</summary>
    /// <param name="path">File name with path</param>
    /// <returns>Just the path as a string</returns>
    public static string JustPath(string path)
    {
      return path.Substring(0, At("\\", path, Occurs("\\", path)) - 1);
    }

    /// <summary>Returns just the file name part of a full path</summary>
    /// <param name="path">The full path to the file</param>
    /// <returns>File name</returns>
    public static string JustFileName(string path)
    {
      var parts = path.Split('\\');
      if (parts.Length > 0)
        return parts[parts.Length - 1];
      return string.Empty;
    }

    /// <summary>Makes sure the secified path ends with a back-slash</summary>
    /// <param name="path">Path</param>
    /// <returns>Path with BS</returns>
    public static string AddBS(string path)
    {
      if (!path.EndsWith("\\"))
        path += "\\";
      return path;
    }
    /// <summary>Receives two strings as parameters and searches for one string within another. If found, returns the beginning numeric position otherwise returns 0</summary>
    /// <example>StringHelper.At("D", "Joe Doe");	//returns 5</example>
    /// <param name="searchFor">String to search for</param>
    /// <param name="searchIn">String to search in</param>
    /// <returns>Position</returns>
    public static int At(string searchFor, string searchIn)
    {
      return searchIn.IndexOf(searchFor) + 1;
    }

    /// <summary>
    /// Receives two strings and an occurrence position (1st, 2nd etc) as parameters and 
    /// searches for one string within another for that position. 
    /// If found, returns the beginning numeric position otherwise returns 0
    /// </summary>
    /// <example>
    /// StringHelper.At("o", "Joe Doe", 1);	//returns 2
    /// StringHelper.At("o", "Joe Doe", 2);	//returns 6
    /// </example>
    /// <param name="searchFor">String to search for</param>
    /// <param name="searchIn">String to search in</param>
    /// <param name="occurrence">The occurrence of the string</param>
    /// <returns>Position</returns>
    public static int At(string searchFor, string searchIn, int occurrence)
    {
      int counter;
      var occured = 0;
      var position = 0;

      //Loop through the string and get the position of the requiref occurrence
      for (counter = 1; counter <= occurrence; counter++)
      {
        position = searchIn.IndexOf(searchFor, position);

        if (position < 0) break;
        //Increment the occured counter based on the current mode we are in
        occured++;

        //Check if this is the occurrence we are looking for
        if (occured == occurrence) return position + 1;
        position++;
      }
      return 0;
    }
    /// <summary>Returns a bool indicating if the first character in a string is an alphabet or not</summary>
    /// <example>StringHelper.IsAlpha("Joe Doe"); // returns true</example>
    /// <param name="expression">Expression</param>
    /// <returns>True or False depending on whether the string only had alphanumeric chars</returns>
    public static bool IsAlpha(string expression)
    {
      //Check if the first character is a letter
      return Char.IsLetter(expression[0]);
    }

    /// <summary>Returns the number of occurrences of a character within a string</summary>
    /// <example>StringHelper.Occurs('o', "Joe Doe"); // returns 2</example>
    /// <param name="character">Search Character</param>
    /// <param name="expression">Expression</param>
    /// <returns>Number of occurrences</returns>
    public static int Occurs(char character, string expression)
    {
      int counter, occured = 0;

      //Loop through the string
      for (counter = 0; counter < expression.Length; counter++)
        //Check if each expression is equal to the one we want to check against
        if (expression[counter] == character)
          //if  so increment the counter
          occured++;
      return occured;
    }

    /// <summary>Returns the number of occurrences of one string within another string</summary>
    /// <example>
    /// StringHelper.Occurs("oe", "Joe Doe"); //returns 2
    /// StringHelper.Occurs("Joe", "Joe Doe"); //returns 1
    /// </example>
    /// <param name="searchString">Search String</param>
    /// <param name="stringSearched">Expression</param>
    /// <returns>Number of occurrences</returns>
    public static int Occurs(string searchString, string stringSearched)
    {
      int position = 0;
      int occured = 0;
      do
      {
        //Look for the search string in the expression
        position = stringSearched.IndexOf(searchString, position);

        if (position < 0) break;
        //Increment the occured counter based on the current mode we are in
        occured++;
        position++;
      } while (true);

      //Return the number of occurrences
      return occured;
    }


  }
}
